#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "Shader.hpp"
#include "MeshRenderer.hpp"
#include "Texture.hpp"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <cmath>


void frame_buff_size_callback(GLFWwindow* window, int width, int height);
void pre_render();
void render();


int main() {

    // GLFW INIT
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow* window = glfwCreateWindow(800, 600, "Hello World", nullptr, nullptr);
    if(window == nullptr) {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwSetFramebufferSizeCallback(window, frame_buff_size_callback);
    glfwMakeContextCurrent(window);

    // GLAD
    if(!gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress))) {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }
    glViewport(0, 0, 800, 600);

    pre_render();

    while (!glfwWindowShouldClose(window)) {
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        render();
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwTerminate();
    return 0;
}

void frame_buff_size_callback(GLFWwindow* window, const int width, const int height) {
    glViewport(0, 0, width, height);
}

Shader* shader;
MeshRenderer* quad_mesh_renderer;
MeshRenderer* cube_mesh_renderer;
Texture* texture1;
Texture* texture2;

void pre_render() {
    shader = new Shader("Assets/Shader/07_CoordinateSystem/shader2.vert", "Assets/Shader/07_CoordinateSystem/shader2.frag");
    texture1 = new Texture("Assets/Texture/t1.png");
    texture2 = new Texture("Assets/Texture/t2.png");
    {
        float vertices[] = {
            //     ---- 位置 ----       ---- 颜色 ----     - 纹理坐标 -
            0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f,   // 右上
            0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f,   // 右下
           -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f,   // 左下
           -0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f    // 左上
        };
        unsigned int indices[] = {
            0, 1, 3, // 第一个三角形
            1, 2, 3  // 第二个三角形
        };
        quad_mesh_renderer = new MeshRenderer(MeshRenderer::vertex_format_position | MeshRenderer::vertex_format_color | MeshRenderer::vertex_format_uv1);
        quad_mesh_renderer->SetShader(shader);
        quad_mesh_renderer->SetVertex(vertices, std::size(vertices));
        quad_mesh_renderer->SetIndices(indices, std::size(indices));
        quad_mesh_renderer->Init();
    }
    {
        float vertices[] = {
            -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
             0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
             0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
             0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
            -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
            -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,

            -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
             0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
             0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
             0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
            -0.5f,  0.5f,  0.5f,  0.0f, 1.0f,
            -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,

            -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
            -0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
            -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
            -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
            -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
            -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

             0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
             0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
             0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
             0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
             0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
             0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

            -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
             0.5f, -0.5f, -0.5f,  1.0f, 1.0f,
             0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
             0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
            -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
            -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,

            -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
             0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
             0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
             0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
            -0.5f,  0.5f,  0.5f,  0.0f, 0.0f,
            -0.5f,  0.5f, -0.5f,  0.0f, 1.0f
        };
        unsigned int indices[6 * 6];
        for(int i = 0; i < 6*6; i++) {
            indices[i] = i;
        }
        cube_mesh_renderer = new MeshRenderer(MeshRenderer::vertex_format_position | MeshRenderer::vertex_format_uv1);
        cube_mesh_renderer->SetVertex(vertices, std::size(vertices));
        cube_mesh_renderer->SetIndices(indices, std::size(indices));
        cube_mesh_renderer->SetShader(shader);
        cube_mesh_renderer->Init();
    }

}

glm::vec3 cubePositions[] = {
    glm::vec3( 0.0f,  0.0f,  0.0f),
    glm::vec3( 2.0f,  5.0f, -15.0f),
    glm::vec3(-1.5f, -2.2f, -2.5f),
    glm::vec3(-3.8f, -2.0f, -12.3f),
    glm::vec3( 2.4f, -0.4f, -3.5f),
    glm::vec3(-1.7f,  3.0f, -7.5f),
    glm::vec3( 1.3f, -2.0f, -2.5f),
    glm::vec3( 1.5f,  2.0f, -2.5f),
    glm::vec3( 1.5f,  0.2f, -1.5f),
    glm::vec3(-1.3f,  1.0f, -1.5f)
  };

void render() {
    {
        /*quad_mesh_renderer->PreDraw();
        // 矩阵操作
        auto modelTransform = glm::mat4(1);
        modelTransform = glm::rotate(modelTransform, glm::radians(-45.0f), glm::vec3(1.0f, 0.0f, 0.0f));

        glm::mat4 viewTransform = glm::mat4(1);
        float z = (sin(glfwGetTime()) + 1) / 2 * -3.0 - 0.3f;
        viewTransform = glm::translate(viewTransform, glm::vec3(0.0f, 0.0f, z));

        glm::mat4 projectionTransform = glm::mat4(1);
        projectionTransform = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f);

        quad_mesh_renderer->GetShader()->SetMat4("model", glm::value_ptr(modelTransform));
        quad_mesh_renderer->GetShader()->SetMat4("view", glm::value_ptr(viewTransform));
        quad_mesh_renderer->GetShader()->SetMat4("projection", glm::value_ptr(projectionTransform));

        texture1->Use(GL_TEXTURE0);
        texture2->Use(GL_TEXTURE1);
        quad_mesh_renderer->GetShader()->SetInt("ourTexture1", 0);
        quad_mesh_renderer->GetShader()->SetInt("ourTexture2", 1);
        quad_mesh_renderer->Draw();*/
    }

    {
        for (int i = 0; i < std::size(cubePositions); i++) {
            cube_mesh_renderer->PreDraw();
            glEnable(GL_DEPTH_TEST);
            // 矩阵操作
            auto modelTransform = glm::mat4(1);
            modelTransform = glm::translate(modelTransform, cubePositions[i]);
            modelTransform = glm::rotate(modelTransform, static_cast<float>(glfwGetTime()), glm::vec3(1.0f, 0.0f, 0.0f));
            modelTransform = glm::rotate(modelTransform, static_cast<float>(glfwGetTime()), glm::vec3(0.0f, 1.0f, 0.0f));
            auto viewTransform = glm::mat4(1);
            float z = (sin(glfwGetTime()) + 1) / 2 * -5.0 - 4.0f;
            viewTransform = glm::translate(viewTransform, glm::vec3(0.0f, 0.0f, z));


            auto projectionTransform = glm::mat4(1);
            projectionTransform = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f);

            cube_mesh_renderer->GetShader()->SetMat4("model", glm::value_ptr(modelTransform));
            cube_mesh_renderer->GetShader()->SetMat4("view", glm::value_ptr(viewTransform));
            cube_mesh_renderer->GetShader()->SetMat4("projection", glm::value_ptr(projectionTransform));

            texture1->Use(GL_TEXTURE0);
            texture2->Use(GL_TEXTURE1);
            cube_mesh_renderer->GetShader()->SetInt("ourTexture1", 0);
            cube_mesh_renderer->GetShader()->SetInt("ourTexture2", 1);

            cube_mesh_renderer->Draw();
        }
    }



}